local chenzui = fk.CreateSkill{
  name = "lvshi__chenzui",
}

Fk:loadTranslationTable{
  ["lvshi__chenzui"] = "陈罪",
  [":lvshi__chenzui"] = "出牌阶段开始时，你可令一名其他角色展示一张手牌，当你于此阶段使用牌点数之和首次大于此牌后，你对其造成1点伤害。若第一张便触发，你<a href='chenzui_xushi'>蓄势此技能</a>并令其弃置其余手牌。",
  ["chenzui_xushi"] = "你不能发动此技能直到洗牌；出牌阶段，若你未于此轮发动过此技能，你可以弃置三张牌，重置此技能",
  ["#lvshi__chenzui-target"] = "陈罪：你可令一名其他角色展示一张手牌",
  ["#lvshi__chenzui-show"] = "陈罪：请展示一张手牌",
  ["#lvshi__chenzui-refresh"] = "陈罪：你可弃置三张牌重置此技能",
  ["@@chenzui_xushi"] = "陈罪 蓄势中",
  ["@lvshi-chenzui-phase"] = "陈罪",

  ["$lvshi__chenzui1"] = "举烽火为号，你我共襄义举！",
  ["$lvshi__chenzui2"] = "老贼久负不臣之心，某，先攻为敬。",
}

chenzui:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(chenzui.name) and player.phase == Player.Play
    and player:getMark("@@chenzui_xushi") == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = table.filter(room:getOtherPlayers(player), function (p) return not p:isKongcheng() end),
      skill_name = "lvshi__chenzui",
      prompt = "#lvshi__chenzui-target",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:addTableMark(player, "chenzui-phase", to.id)
    if not to:isKongcheng() then
      local cards = room:askToCards(to, {
        min_num = 1,
        max_num = 1,
        skill_name = chenzui.name,
        include_equip = false,
        prompt = "#lvshi__chenzui-show",
        cancelable = false,
      })
      if #cards > 0 then
        to:showCards(cards)
        local number = Fk:getCardById(cards[1]).number
        room:setPlayerMark(player, "chenzui-card-phase", cards[1])
        local n = 0
        room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
          local use = e.data
          if use.from == player then
            n = n + use.card.number
          end
        end, Player.HistoryPhase)
        room:setPlayerMark(player, "@lvshi-chenzui-phase", n .. "/".. number)
      end
    end
  end,
})

chenzui:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(chenzui.name) and #player:getTableMark("chenzui-phase") ~= 0
    and player:getMark("chenzui_used-phase") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
      local use = e.data
      if use.from == player then
        n = n + use.card.number
      end
    end, Player.HistoryPhase)
    local id = player:getMark("chenzui-card-phase")
    local number = Fk:getCardById(id).number
    room:setPlayerMark(player, "@lvshi-chenzui-phase", n .. "/".. number)
    if n > number then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if table.contains(player:getTableMark("chenzui-phase"), p.id) and p:isAlive() then
          room:addPlayerMark(player, "chenzui_used-phase")
          room:damage({
            from = player,
            to = p,
            damage = 1,
            skillName = chenzui.name,
          })
          if #room.logic:getEventsOfScope(GameEvent.UseCard, 999, function(e)
            return e.data.from == player
          end, Player.HistoryTurn) == 1 then
            room:addPlayerMark(player, "@@chenzui_xushi")
            local cards = table.filter(p:getCardIds("h"), function (cd)
              return player:getMark("chenzui-card-phase") ~= cd end)
            if #cards > 0 then
              room:throwCard(cards, chenzui.name, p, p)
            end
          end
        end
      end
    end
  end,
})

chenzui:addEffect(fk.AfterDrawPileShuffle, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(chenzui.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@chenzui_xushi", 0)
  end,
})

chenzui:addEffect("active", {
  prompt = "#lvshi__chenzui-refresh",
  anim_type = "special",
  card_num = 3,
  target_num = 0,
  can_use = function(self, player)
    return player:getMark("@@chenzui_xushi") > 0 and player:usedSkillTimes(chenzui.name, Player.HistoryRound) == 0
  end,
  card_filter = function (self, player, to_select, selected)
    if player:prohibitDiscard(to_select) then return false end
    return #selected < 3
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected_cards == 3
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:throwCard(effect.cards, chenzui.name, player, player)
    room:setPlayerMark(player, "@@chenzui_xushi", 0)
  end,
})

return chenzui